package one.stand.util;

import lombok.Builder;
import lombok.Data;

import java.util.Objects;

/**
 * @author cjr
 */
@Data
public class RequestContext {
    private boolean isLogin;
    private boolean isCard;

    private Request request;
    private User user;

    @Data
    @Builder
    public static class Request {
        private String equCode;
        private String version;
        private String clientPlatform;
        private String companyKey;
        private String ip;
        private String token;
    }

    @Data
    @Builder
    public static class User {
        private String companyKey;
        private String loginProgramName;

        private Integer userId;
        private Integer companyId;
        private Integer cardId;
        private Integer roleId;
        private String sessionKey;

    }

    public boolean isSelf(Integer infoUserId) {
        return Objects.nonNull(user) && getUser().getUserId().equals(infoUserId);
    }

    public boolean isSameCompany(Integer infoCompanyId) {
        return Objects.nonNull(user) && Objects.nonNull(user.getCompanyId()) && getUser().getCompanyId().equals(infoCompanyId);
    }

    public boolean isSameCard(Integer infoCardId) {
        return Objects.nonNull(user) && Objects.nonNull(user.getCardId()) && getUser().getCardId().equals(infoCardId);
    }

    public boolean isBoss() {
        return Objects.nonNull(user) && Objects.nonNull(user.getRoleId()) && user.getRoleId() > 0;
    }

    public boolean isIos() {
        return Objects.nonNull(request) && "iOS".equals(request.getClientPlatform());
    }
}
